﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Admin.Push
{
    public class PushLogService : IPushLogService
    {
        Domain.Uow.IRepository<Domain.Entity.PushLog> logRepository;

        Domain.Uow.IRepository<Domain.Entity.PushContents> contentRepository;

        Domain.Uow.IRepository<Domain.Entity.MobileClient> clientRepository;

        public PushLogService(Domain.Uow.IRepository<Domain.Entity.PushLog> log,
            Domain.Uow.IRepository<Domain.Entity.PushContents> content,
            Domain.Uow.IRepository<Domain.Entity.MobileClient> client)
        {
            if (log == null)
            {
                throw new ArgumentNullException("logRepository is null");
            }
            if (content == null)
            {
                throw new ArgumentNullException("contentRepository is null");
            }
            if (client == null)
            {
                throw new ArgumentNullException("clientRepository is null");
            }
            logRepository = log;
            contentRepository = content;
            clientRepository = client;
        }

        public Domain.Entity.PushLog Add(Domain.Entity.PushLog add, Domain.Entity.User operUser)
        {
            if (add == null) return add;
            logRepository.Add(add);
            logRepository.Uow.Commit();
            return add;
        }

        public bool Delete(Domain.Entity.PushLog delete, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                delete.State = false;
                logRepository.Modify(delete);
                logRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(Domain.Entity.PushLog delete, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                logRepository.Delete(delete);
                logRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Delete(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = logRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                item.State = false;
                logRepository.Modify(item);
            }
            try
            {
                logRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = logRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                logRepository.Delete(item);
            }
            try
            {
                logRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Modify(Domain.Entity.PushLog modify, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                logRepository.Modify(modify);
                logRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public Domain.Entity.PushLog Get(int id)
        {
            var data = logRepository.Get(id);
            return data;
        }

        public IQueryable<Domain.Entity.PushLog> GetList()
        {
            var data = logRepository.GetList();
            return data;
        }

        public IQueryable<Application.Admin.Dto.PushLogDto> GetPushLogDtoTrueState(int type = 0)
        {
            var data = logRepository.GetList(e => e.State == true);
            if (type > 0)
            {
                data = data.Where(e => e.Result == type);
            }
            var res = from log in data
                      join client in clientRepository.GetList() on log.ClientId equals client.Id
                      join content in contentRepository.GetList() on log.ContentId equals content.Id
                      select new Application.Admin.Dto.PushLogDto
                       {
                           AddTime = log.AddTime,
                           ClientId = log.ClientId,
                           ClientName = client != null ? client.Name : "未知设备",
                           Content = content != null ? content.Content : "没有内容",
                           ContentId = log.ContentId,
                           Id = log.Id,
                           IpAddress = log.IpAddress,
                           Result = log.Result,
                           ResultType = log.Result == 1 ? "成功" :
                           (log.Result == 2 ? "失败" :
                           (log.Result == 3 ? "错误的客户端" :
                           (log.Result == 4 ? "内容太长" :
                           (log.Result == 5 ? "推送错误" : "未知错误")))),
                           SortNum = log.SortNum,
                           State = log.State
                       };
            return res.OrderByDescending(e => e.Id);
        }

    }
}
